home *** CD-ROM | disk | FTP | other *** search
- //--------------------------------------------------------------------------------------------
- // File Handling Code
- //
- // by Philip McBride
- // parts taken from Apple DTS sample code
- //
- //--------------------------------------------------------------------------------------------
-
-
- #include "GameControls.h"
- #include "extern.h"
- #include "draw.h"
- #include "file.h"
- #include "lights.h"
- #include "camera.h"
-
-
- //--------------------------------------------------------------------------------------------
- // Open a File for Reading
- //
- OSErr MyOpenFile(FSSpec *theFile, DocumentPtr theDocument)
- { OSErr result;
- short refNum;
- FInfo fndrInfo ;
-
- FSpGetFInfo( theFile, &fndrInfo ) ;
-
- // Open file
- if ((result = FSpOpenDF(theFile, fsRdWrPerm, &refNum)) != noErr)
- return(result);
-
- // Assign file info to document
- theDocument->fRefNum = refNum;
- theDocument->theFileSpec = *theFile;
-
- // Set cursor and read model file
- SetCursor(*GetCursor(watchCursor));
- result = MyReadDocumentFile( theDocument);
-
- // Show window w/ title and reset cursor
- SetWTitle((WindowPtr)theDocument->theWindow, theFile->name);
- ShowWindow((WindowPtr)theDocument->theWindow);
- SetCursor(&qd.arrow);
-
- return(result);
- }
-
- //--------------------------------------------------------------------------------------------
- // Setup 3D Storage for Reading
- //
- OSErr MyReadDocumentFile(DocumentPtr theDocument)
- {
- TQ3StorageObject storage;
- TQ3FileObject fd;
- TQ3Object objects = nil;
- TQ3Status status;
-
- // create new storage and file objects
- storage = Q3MacintoshStorage_New( theDocument->fRefNum );
- if (storage == nil)
- goto bail;
- fd = Q3File_New();
- if (fd == nil)
- goto bail;
-
- // associate the storage with the file
- Q3File_SetStorage(fd, storage);
- Q3Object_Dispose(storage);
-
- // read the drawable objects from the file object into a new group
- status = MyReadScene(fd, theDocument);
- if (status == kQ3Failure)
- goto bail;
-
- Q3Object_Dispose(fd);
-
- return(noErr);
-
- bail:
- Q3Object_Dispose(fd);
-
- return(fnOpnErr);
- }
-
- //--------------------------------------------------------------------------------------------
- // Read the 3D scene into Memory (create lights and camera if not included)
- //
- TQ3Status MyReadScene(TQ3FileObject file, DocumentPtr theDocument)
- {
- TQ3Object object;
- TQ3Boolean isEOF;
- TQ3ViewObject view;
- TQ3Object model = NULL;
- TQ3GroupObject lightGroup = NULL;
- TQ3Object viewHints = NULL;
- TQ3CameraObject camera = NULL;
- TQ3AttributeSet attributeSet = NULL;
- TQ3RendererObject renderer = NULL;
-
- // Create new model and get view
- model = Q3DisplayGroup_New();
- if (!model)
- return kQ3Failure;
- theDocument->documentGroup = model;
- view = theDocument->theView;
-
- SetCursor(*GetCursor(watchCursor));
-
- // Open file for reading
- if (Q3File_OpenRead(file, NULL) != kQ3Success) {
- SetCursor(&qd.arrow);
- return kQ3Failure;
- }
-
- // Collect all drawable objects (into model) and any viewhints
- while ((isEOF = Q3File_IsEndOfFile(file)) == kQ3False) {
- object = Q3File_ReadObject(file);
-
- if (Q3Object_IsDrawable(object)) {
-
- Q3Group_AddObject(model, object);
-
- } else if (Q3Object_IsType(object, kQ3SharedTypeViewHints)) {
-
- if (viewHints == NULL)
- {
- viewHints = object;
- theDocument->viewHints = viewHints;
- object = NULL;
- }
-
- }
-
- if (object != NULL)
- Q3Object_Dispose(object);
- }
-
- // If we found an error while reading, dispose of objects and exit with error.
- if (Q3Error_Get(NULL) != kQ3ErrorNone) {
-
- if (model != NULL) {
- Q3Object_Dispose(model);
- model = NULL;
- theDocument->documentGroup = NULL;
- }
-
- if (viewHints != NULL) {
- Q3Object_Dispose(viewHints);
- viewHints = NULL;
- theDocument->viewHints = NULL;
- }
- return (kQ3Failure);
- }
-
- // Add lights to view if found. Otherwise create default ones.
- if (viewHints)
- Q3ViewHints_GetLightGroup((TQ3ViewHintsObject)viewHints, &lightGroup);
- if (lightGroup) {
- Q3View_SetLightGroup(view, lightGroup);
- Q3Object_Dispose(lightGroup);
- } else {
- MyNewLights(theDocument);
- }
-
- // Add camera to view if found. Otherwise create default one.
- if (viewHints)
- Q3ViewHints_GetCamera((TQ3ViewHintsObject)viewHints, &camera);
- if (camera) {
- Q3View_SetCamera(view, camera);
- MyGetCameraData(theDocument, camera); // setup the camera data
- Q3Object_Dispose(camera);
- } else {
- camera = MyNewCamera( theDocument->theWindow ) ;
- Q3View_SetCamera(view, camera );
- MyGetCameraData(theDocument, camera); // setup the camera data
- Q3Object_Dispose(camera);
- }
-
- // Add a couple other viewhints found to view (you may want to add the
- // rest including all the drawcontext hints, etc.).
- if (viewHints)
- Q3ViewHints_GetAttributeSet((TQ3ViewHintsObject)viewHints, &attributeSet);
- if (attributeSet) {
- Q3View_SetDefaultAttributeSet(view, attributeSet);
- Q3Object_Dispose(attributeSet);
- }
- if (viewHints)
- Q3ViewHints_GetRenderer((TQ3ViewHintsObject)viewHints, &renderer);
- if (renderer) {
- Q3View_SetRenderer(view, renderer);
- Q3Object_Dispose(renderer);
- }
-
- // Initialize Delta factors based on model read.
- MyInitDeltaFactors(theDocument);
-
- Q3File_Close(file);
-
- if (isEOF == kQ3False) {
- if (model != NULL) {
- Q3Object_Dispose(model);
- model = theDocument->documentGroup = NULL;
- }
-
- SetCursor(&qd.arrow);
- return kQ3Failure;
- }
-
- SetCursor(&qd.arrow);
- return kQ3Success;
- }
-
-
-
-
-